home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0705.dms / q0705.adf / Amiga / Appendices / BinaryOperators.doc < prev    next >
Text File  |  1992-07-29  |  7KB  |  236 lines

  1. 8    BINARY OPERATORS
  2.  
  3. 8.1  INTRODUCTION
  4.  
  5. One of the advantages with C compared to other high level
  6. languages is that you are allowed to deep down into the
  7. computer and directly modify bits and bytes. Some processes
  8. that are normally very complicated and tideous to do can
  9. sometimes easily be solved by working directly with the
  10. memory. The code will then be much smaller, faster, and
  11. actually also (normally) much easier to understand.
  12.  
  13. To be able to manipulate single bits can sometimes be very
  14. handy and sometimes even necessary. A "bit" is the smalest
  15. item in the computer and can only be on (1) or off (0). A
  16. "group" of eight bits is usually reffered as one "byte", and
  17. can represent 256 values (2^8=256). A group of sixteen bits
  18. (two bytes) is called one "word". A group of 32 bits (four
  19. bytes or two words) is called one "long", and finally a group
  20. of 64 bits (eight bytes or four words of two long) is called
  21. "double". See appendix E "Data Types" for more information
  22. about these data types.
  23.  
  24. Usually you are working with the data types mentioned above
  25. (bytes, words and so on), but you can directrly work with
  26. single bits. I will in this chapter try to explain how you
  27. can manipulate single bits, which operators you may use, and
  28. what effect they have.
  29.  
  30. 8.2  OPERATORS
  31.  
  32. There exist six different binary operators in C:
  33.   1. Bitwise AND (&)
  34.   2. Bitwise OR (|)
  35.   3. Bitwise EXCL OR (^)
  36.   4. Bitwise INVERT (~)
  37.   5. LEFT SHIFT (<<)
  38.   6. RIGHT SHIFT (>>)  
  39.  
  40. I will below give a short description for each operator
  41. together with some examples. When I use the word "on" I mean
  42. the bit is 1, and the word "off" when the bit is 0.
  43.  
  44.  
  45.  
  46. 8.2.1  (&) BITWISE AND
  47.  
  48. The bitwise operator "&" is used to compare each bit in the
  49. first data field with the corresponding bit in the second bit
  50. field. If both bits are on (1 and 1), the result is 1. In all
  51. other cases, one of the bits is on and the other is off, or
  52. both is off, the result is 0. The "truth table" for AND looks
  53. like this:
  54.  
  55.   AND (&)
  56.   ----------
  57.   0 & 0 -> 0 
  58.   0 & 1 -> 0
  59.   1 & 0 -> 0
  60.   1 & 1 -> 1
  61.  
  62. For example: (11101000) & (10111001) -> (10101000)
  63.  
  64.     (11101000)
  65.   & (10111001) 
  66.   ------------
  67.     (10101000)
  68.  
  69.  
  70. This operator is very useful if you whant to turn some bits off,
  71. but keep others unchanged. Say you have the field (10110011),
  72. and you what to make sure all last four bits are off, but the
  73. first four unchanged. What you have to to is to create a "mask"
  74. which would look like this (11110000). If you then use the
  75. binary operator AND on the field you want to change together
  76. with this "mask", the new field will have all last four bits
  77. turned off, but leave the first four unchanged (10110000).
  78.  
  79.     (10110011) The field you want to change
  80.   & (11110000) The "mask"
  81.   ------------
  82.     (10110000) Result
  83.  
  84. In 'C' it could look something like this: (UBYTE is declared
  85. in header file "exec/types.h" and can be replaced with
  86. "unsigned char".)
  87.  
  88.   UBYTE my_flag_field = 0xB3; /* binary: (10110011) */
  89.   UBYTE my_mask = 0xF0;      /* binary: (11110000) */
  90.   UBYTE my_new_flag_field;
  91.  
  92.   my_new_flag_field = my_flag_field & my_mask;
  93.  
  94.  
  95.  
  96. 8.2.2  (|) BITWISE OR
  97.  
  98. The bitwise operator "|" is used to compare each bit in the
  99. first data field with the corresponding bit in the second bit
  100. field. If both bits or one of them are on, the result is 1. It
  101. is only when both bits are off the result will be 0. The "truth
  102. table" for OR looks like this:
  103.  
  104.   OR (|)
  105.   ----------
  106.   0 | 0 -> 0 
  107.   0 | 1 -> 1
  108.   1 | 0 -> 1
  109.   1 | 1 -> 1
  110.  
  111. For example: (11101000) | (10111001) -> (11111001)
  112.  
  113.     (11101000)
  114.   | (10111001) 
  115.   ------------
  116.     (11111001)
  117.  
  118.  
  119. This operator is normally used when you want to add (turn on)
  120. some bits but leave all others unchanged. As you probably
  121. already have seen in several examples in this manual, the OR
  122. operator can be used to add "flags" (bits) to a field. 
  123.  
  124. A "flag" is actually a name which represents one single bit.
  125. For example, the flag "WINDOWCLOSE" is declared in header
  126. file "intuition/intuition.h" to 0x0008 (0000000000001000),
  127. and WINDOWDRAG to 0x0002 (0000000000000010). If you want to
  128. add these to flags you simply put the OR operator between
  129. them.
  130.  
  131. In 'C' it would look something like this:
  132.  
  133.   WORD my_window_flags;
  134.   my_window_flags = WINDOWCLOSE | WINDOWDRAG;
  135.  
  136.  
  137.  
  138. 8.2.3  (^) BITWISE EXCLUSIVE OR
  139.  
  140. The bitwise operator "^" is used to compare each bit in the
  141. first data field with the corresponding bit in the second bit
  142. field. If one bit is on and the other off the result will be 1.
  143. If both bits are on or both bits are off the result will be 0.
  144. The "truth table" for EXCLUSIVE OR looks like this:
  145.  
  146.   EXCL OR (^)
  147.   -----------
  148.   0 ^ 0 -> 0 
  149.   0 ^ 1 -> 1
  150.   1 ^ 0 -> 1
  151.   1 ^ 1 -> 0
  152.  
  153. For example: (11101000) ^ (10111001) -> (01010001)
  154.  
  155.     (11101000)
  156.   | (10111001) 
  157.   ------------
  158.     (01010001)
  159.  
  160.  
  161.  
  162. 8.2.4  (~) BITWISE INVERT
  163.  
  164. The bitwise operator "~" is used to invert each bit in the
  165. data field. If the bit was on it will be turned off, and if
  166. the bit was off it will be turned on. The "truth table" for
  167. INVERT looks like this:
  168.  
  169.   INVERT (~)
  170.   ----------
  171.   ~ 1 -> 0
  172.   ~ 0 -> 1
  173.  
  174. For example: ~(11101000) -> (00010111)
  175.  
  176.   ~ (11101000)
  177.   ------------
  178.     (00010111)
  179.  
  180.  
  181.  
  182. 8.2.5  (<<) LEFT SHIFT
  183.  
  184. The operator "<<" is used to move all bits in a field a
  185. specified number of steps to the left. The number of steps is
  186. stated just after the operator. The right part of the field
  187. will be replaced with 0:s.
  188.  
  189. For example: (11101000) << 2  ->  (10100000)
  190.  
  191.  
  192. This operator is very useful when you want to multiply a value
  193. with another value which is a power of two. This operation is
  194. very easy for the computer and therefore extremely fast. To
  195. multiply a value with two, move the bits one step to the left.
  196. To multiplay a value with four, move the bits two steps to the
  197. left and so on.
  198.  
  199.   LEFT SHIFT (<<)
  200.   --------------------------------
  201.   value << 1  same as:  value *  2
  202.   value << 2  same as:  value *  4
  203.   value << 3  same as:  value *  8
  204.   value << 4  same as:  value * 16
  205.   value << 5  same as:  value * 32
  206.   and so on...
  207.  
  208.  
  209.  
  210. 8.2.6  (>>) RIGHT SHIFT
  211.  
  212. The operator ">>" is used to move all bits in a field a
  213. specified number of steps to the right. The number of steps is
  214. stated just after the operator. The left part of the field
  215. will be replaced with 0:s.
  216.  
  217. For example: (11101000) >> 2  ->  (00111010)
  218.  
  219.  
  220. This operator is very useful when you want to divide a value
  221. with another value which is a power of two. This operation is
  222. very easy for the computer and therefore extremely fast. To
  223. divide a value with two, move the bits one step to the right.
  224. To divide a value with four, move the bits two steps to the
  225. right and so on.
  226.  
  227.   RIGHT SHIFT (>>)
  228.   --------------------------------
  229.   value >> 1  same as:  value /  2
  230.   value >> 2  same as:  value /  4
  231.   value >> 3  same as:  value /  8
  232.   value >> 4  same as:  value / 16
  233.   value >> 5  same as:  value / 32
  234.   and so on...
  235.  
  236.